<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class School_page extends MY_Controller {

	public $left_menu_min;

	function __construct(){
	parent::__construct();

	$this->left_menu_min = array("menu" => "网校设置", "curr" => '/school', "list" => array("学校设置" => "/school", "学校资讯" => "/news", "帮助中心" => "/help", "友情连接" => "/friend_link", "网点管理" => "/wangdian", "问答管理" => "/question_topic", "客服设置" => "/kefu"));
	  $this->smarty->assign("left_menu_min", $this->left_menu_min);
	  $this->smarty->assign("menu", 'school');

	}


	function index($search = 0, $limit = 0){
		$offset = 12;
		if($this->input->post()){
			$search = $this->input->post('search',TRUE)? urldecode($this->input->post('search',TRUE)) : 0;
			$limit = 0;
		}else{
			$search = urldecode($search);
			$page_config['uri_segment'] = 4; 
		}
		$data = array('search' => $search, 'limit' => $limit, "school_id" => $this->user_data['school_id']);
		$this->load->model('School_pages');

		$this->load->library('pagination');
		$list = $this->School_pages->get_list($limit, $offset, $data);
		$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		$page_config['total_rows'] = $list['count'];
		$page_config['per_page'] = $offset;
		$page_config['base_url'] = '/school_page/index/'.$data['search'];
		$this->pagination->initialize($page_config); 
		$pagination = $this->pagination->create_links();

		$this->smarty->assign("current_url", $current_url);
		$this->smarty->assign("data", $data);
		$this->smarty->assign("list", $list['result']);
		$this->smarty->assign("pagination", $pagination);
		$this->smarty->display("school/page_list.html");
	}

	function add(){
		$this->smarty->assign("post_url", '/school_page/add');
		$this->smarty->display("school/page_save.html");
	}

	function edit($id){
		if($id > 0){
			$info = get_field_by_where("o2o_school_pages", '*', "id='{$id}' AND school_id = '{$this->user_data['school_id']}'");
			$this->smarty->assign("info", $info);
			$this->smarty->display("school/page_save.html");
		}else{
			show_message($_SERVER['HTTP_REFERER']);
		}
	}

	function save(){
		$post_data = $this->input->post();
		if($post_data){
			$page_data['page_title'] 	= trim($post_data['page_title']);
			$page_data['type'] 			= !empty($post_data['type']) ? $post_data['type'] : 1;
			if(!empty($post_data['url'])){
				if(!strstr($post_data['url'], 'http://')) {
					$page_data['url'] = "http://".$post_data['url'];
				}else{
					$page_data['url'] = $post_data['url'];
				}
			}
			$page_data['page_tags'] 	= trim($post_data['page_tags']);
			$page_data['page_intro'] 	= trim($post_data['page_intro']);
			$page_data['content'] 		= trim($post_data['content']);
			$page_data['sort'] 			= intval($post_data['sort']);
			$page_data['status'] 		= isset($post_data['status']) ? $post_data['status'] : 0;
			$page_data['show_menu'] 	= isset($post_data['show_menu']) ? $post_data['show_menu'] : 0;
			if(intval($post_data['id']) > 0){
				$id = intval($post_data['id']);
				$this->db->where("id", $id);
				$this->db->update("o2o_school_pages", $page_data);
			}else{
				$page_data['add_time'] = time();
				$page_data['school_id'] = $this->user_data['school_id'];
				$this->db->insert("o2o_school_pages",$page_data);
			}
			show_message($_SERVER['HTTP_REFERER']);
		}else{
			show_message($_SERVER['HTTP_REFERER'], "非法操作");
		}
	}

	function delete($id = 0){
		if($_POST){
			$this->load->model('Advertisings');
			$ids = $this->input->post('ids');
			if(count($ids) > 0){
				delete_data($ids, "o2o_school_pages");
				show_message($_SERVER['HTTP_REFERER'], "删除成功");
			}else{
				show_message($_SERVER['HTTP_REFERER'], "您没有选中任何数据");exit;
			}
		}elseif( (int) $id > 0){
			delete_data((array)$id, "o2o_school_pages");
			show_message($_SERVER['HTTP_REFERER'], "删除成功");
		}
	}
}